package unimelb.distSystems.game.players;

import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.math.Vector3f;

public class LocalPlayer extends PlayerCar {
    private float steeringValue = 0;
    private float accelerationValue = 0;
    
    public LocalPlayer(long player_id)
    {
    	super(player_id);
    }
    
    public void takeAction(String binding, boolean value, float tpf)
    {    	
    	if (binding.equals("Lefts")) {
            if (value) {
                steeringValue += .5f;
            } else {
                steeringValue += -.5f;
            }
            player_control.steer(steeringValue);
        } else if (binding.equals("Rights")) {
            if (value) {
                steeringValue += -.5f;
            } else {
                steeringValue += .5f;
            }
            player_control.steer(steeringValue);
        } 
    	//note that our fancy car actually goes backwards..
        else if (binding.equals("Ups")) {
            if (value) {
                accelerationValue -= 800;
            } else {
                accelerationValue += 800;
            }
            player_control.accelerate(accelerationValue);
            player_control.setCollisionShape(CollisionShapeFactory.createDynamicMeshShape(geo_tool.findGeom(carNode, "Car")));
        } else if (binding.equals("Downs")) {
            if (value) {
            	accelerationValue += 1600;
            } else {
        		accelerationValue -= 1600;
            }           
            player_control.accelerate(accelerationValue);
            player_control.setCollisionShape(CollisionShapeFactory.createDynamicMeshShape(geo_tool.findGeom(carNode, "Car")));
        } 
        /*
        else if (binding.equals("Reset")) {
            if (value) {
                System.out.println("Reset");
                player.setPhysicsLocation(Vector3f.ZERO);
                player.setPhysicsRotation(new Matrix3f());
                player.setLinearVelocity(Vector3f.ZERO);
                player.setAngularVelocity(Vector3f.ZERO);
                player.resetSuspension();
            } else {
            }
        }
        */
    }
    
    public float getDefaultHorizontalRotationAngle()
    {
    	float D_X=player_control.getForwardVector(new Vector3f()).getX();
    	float D_Z=player_control.getForwardVector(new Vector3f()).getZ();
    	float D=(float)Math.atan(D_Z/D_X);
    	
    	if(D_X<0 && D_Z>0)
    	{
    		D-=Math.PI;
    	}
    	if(D_X<0 && D_Z<0)
    	{
    		D+=Math.PI;
    	}
    	
    	return D;
    }
}
